//
//  DYPageTitleView.swift
//  Douyu_Swift
//
//  Created by Albert Zhu on 16/11/1.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

private let kScrollLineH: CGFloat = 2
private let kDeltaColor: (CGFloat, CGFloat, CGFloat) = (170, 43, -85)
private let kSelectedColor: (CGFloat, CGFloat, CGFloat) = (255, 128, 0)

protocol DYPageTitleViewDelegate: class {
    func pageTitleView(_ titleView: DYPageTitleView, selectedIndex: Int)
}

class DYPageTitleView: UIView {
    
    weak var delegate: DYPageTitleViewDelegate?
    var index = 0
    var titles: [String]
    lazy var scrollView: UIScrollView = {
        
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.isPagingEnabled = false
        scrollView.bounces = false
        scrollView.frame = self.bounds
        
        return scrollView
    }()
    
    lazy var scrollLine: UIView = {
        let scrollLine = UIView()
        scrollLine.backgroundColor = UIColor.orange
        
        return scrollLine
    }()
    
    var titleLabels = [UILabel]()
    
    init(frame: CGRect, titles: [String]) {
        
        self.titles = titles
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension DYPageTitleView {
    ///  滑动collectionView设置滑块滑动和label颜色渐变
    ///
    /// - parameter progress: collectionView的contentOffsetX和collectionView宽度之比
    func setLine(progress: CGFloat) {
        scrollLine.frame.origin.x = scrollLine.bounds.width * progress
        
        let idx = Int(progress)
        let deltaA = progress - CGFloat(idx)
        let deltaB = CGFloat(idx + 1) - progress
        let labela = titleLabels[idx]
        var labelb: UILabel?
        if idx + 1 < titleLabels.count {
            labelb = titleLabels[idx + 1]
        }
        
        if CGFloat(idx) == progress {
            
            if index == idx {return}
            
            let oldLabel = titleLabels[index]
            let newLabel = titleLabels[idx]
            
            newLabel.textColor = UIColor.orange
            oldLabel.textColor = UIColor.darkGray
            
            index = idx
            
            return
        }
    
        labela.textColor = UIColor(az_color_red: kSelectedColor.0 - kDeltaColor.0 * deltaA, green: kSelectedColor.1 - kDeltaColor.1 * deltaA, blue: kSelectedColor.2 - kDeltaColor.2 * deltaA)
        labelb?.textColor = UIColor(az_color_red: kSelectedColor.0 - kDeltaColor.0 * deltaB, green: kSelectedColor.1 - kDeltaColor.1 * deltaB, blue: kSelectedColor.2 - kDeltaColor.2 * deltaB)
    }
}

// MARK: - UI相关
extension DYPageTitleView {
    func setupUI() {
        
        addSubview(scrollView)
        setupTitleLabels()
        setupBottomMenuAndScrollLine()
    }
    
    func setupTitleLabels() {
        
        let labelW: CGFloat = frame.width / CGFloat(titles.count)
        let labelH: CGFloat = frame.height - kScrollLineH
        let labelY: CGFloat = 0
        
        for (index, title) in titles.enumerated() {
            let label = UILabel()
            label.text = title
            label.tag = index
            label.font = UIFont.systemFont(ofSize: 16)
            label.textColor = UIColor.darkGray
            label.textAlignment = .center
            label.isUserInteractionEnabled = true
            
            let tap = UITapGestureRecognizer(target: self, action: #selector(labelClick))
            label.addGestureRecognizer(tap)
            
            let labelX: CGFloat = labelW * CGFloat(index)
            label.frame = CGRect(x: labelX, y: labelY, width: labelW, height: labelH)
            
            scrollView.addSubview(label)
            
            titleLabels.append(label)
        }
    }
    
    @objc private func labelClick(ges: UIGestureRecognizer) {
        
        guard let label = ges.view as? UILabel else {return}
        if index == label.tag {return}
        
        let oldLabel = titleLabels[index]
        
        label.textColor = UIColor.orange
        oldLabel.textColor = UIColor.darkGray
        
        index = label.tag
        
        let scrollPosition = CGFloat(label.tag) * scrollLine.bounds.width
        
        UIView.animate(withDuration: 0.25) {
            self.scrollLine.frame.origin.x = scrollPosition
        }
        
        delegate?.pageTitleView(self, selectedIndex: index)
    }
    
    private func setupBottomMenuAndScrollLine() {
        
        let kLineH: CGFloat = 0.5
        
        let bottomLine = UIView(frame: CGRect(x: 0, y: frame.height - kLineH, width: frame.width, height: kLineH))
        bottomLine.backgroundColor = UIColor.lightGray
        
        addSubview(bottomLine)
        
        let label = titleLabels[0]
        label.textColor = UIColor.orange
        scrollView.addSubview(scrollLine)
        scrollLine.frame = CGRect(x: label.frame.origin.x, y: frame.height - kScrollLineH, width: label.frame.width, height: kScrollLineH)
    }
}
